<%--
  Created by IntelliJ IDEA.
  User: 123
  Date: 2021/5/8
  Time: 23:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户注册</title>
    <link type="text/css" rel="stylesheet" href="../../Static-Resources/css/helloLogin.css" />
    <script type="text/javascript" src="../../Static-Resources/js/jquery-3.4.1.js"></script>
</head>
<body>
<div class="a">
<div class="b"></div>
<div class="c">
    <div class="d">
        <h1>用户注册</h1>
        <form action="#" method="post" id="addUser">
        <input type="text" class="e" placeholder="用户名" id="byName" onblur="checkName()" required>
        <input type="text" class="e" placeholder="电话" id="byPhone" required>
        <input type="text" class="e" placeholder="邮箱" id="byEmail" required>
        <input type="text" class="e" placeholder="邀请码" id="byAdmID" required>
        <input type="button" class="g" value="注册" style="bottom: 20px" onclick="sub()">
        </form>
    </div>
</div>
</div>
<script type="text/javascript">
    var checker = false;
    function checkName(){
        $.ajax({
            url:"${pageContext.request.contextPath}/check/"+$('#byName').val(),
            success : function (data){
                if(data){
                    checker = true;
                    alert("名字可用")
                }
                else{
                    checker = false ;
                   alert("名字已有")
                }
            }
        })
    }

    //包装JSON对象传回去
    function sub(){
        if(checker){
            function getData() {
                var json={
                    userID : "" ,
                    userName: $('#byName').val(),
                    userPhone : $('#byPhone').val(),
                    userEmail : $('#byEmail').val(),
                    userTime : "",
                    userRank : "",
                    userStatus : "",
                    admID : $('#byAdmID').val()
                }
                return json;
            }
            $.ajax({
                    type:'get',
                    dataType:'json',
                    url:"${pageContext.request.contextPath}/add/user",
                    data :{"user" : JSON.stringify(getData())},
                    contentType: "application/json"
                },
            );
            $(window).attr('location',"${pageContext.request.contextPath }/login");
        }
        else
        {
            alert("姓名违法");
        }

    }
</script>
</body>
</html>
